package com.xncoding.websocket.WebMvc.demo0.handler;

import org.springframework.lang.NonNull;
import org.springframework.web.socket.*;

public class ServletWebSocketServerHandler implements WebSocketHandler {

   //发送文本消息
//session.sendMessage(new TextMessage(CharSequence message);
////发送二进制消息
//session.sendMessage(new BinaryMessage(ByteBuffer message));
////发送ping
//session.sendMessage(new PingMessage(ByteBuffer message));
////发送pong
//session.sendMessage(new PongMessage(ByteBuffer message));

    @Override
    public void afterConnectionEstablished(@NonNull WebSocketSession session) throws Exception {
        //连接建立
        System.out.println("连接建立"+session.getId());
        session.sendMessage(new TextMessage("webmvcsocket建立链接"));
    }

    @Override
    public void handleMessage(@NonNull WebSocketSession session, @NonNull WebSocketMessage<?> message) throws Exception {
        //接收消息
        System.out.println("接收消息"+session.getId());
        System.out.println("接收消息"+message.toString());
        session.sendMessage(new TextMessage(message.getPayload()+"服务器收到了"));
    }

    @Override
    public void handleTransportError(@NonNull WebSocketSession session, @NonNull Throwable exception) throws Exception {
        exception.printStackTrace();
        //异常处理
        System.out.println("接收消息"+session.getId());
        System.out.println("异常处理");
    }

    @Override
    public void afterConnectionClosed(@NonNull WebSocketSession session, @NonNull CloseStatus closeStatus) throws Exception {
        //连接关闭
        System.out.println("连接关闭"+session.getId());
    }

    @Override
    public boolean supportsPartialMessages() {
        //是否支持接收不完整的消息
        return false;
    }
}